package com.xinchuang.service.targetSystem.Impl.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.common.KeyWordSystem;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.target.TargetSpecialAttribute;
import com.xinchuang.entity.targetSystem.system.TargetType;
import com.xinchuang.mapper.target.TargetSpecialAttributeMapper;
import com.xinchuang.mapper.target.TargetSubTableMapper;
import com.xinchuang.mapper.targetSystem.TargetMapper;
import com.xinchuang.mapper.targetSystem.TargetTypeMapper;
import com.xinchuang.mapper.targetSystem.config.SystemConfigMapper;
import com.xinchuang.mapper.targetSystem.config.TargetElseMapper;
import com.xinchuang.entity.targetSystem.config.SystemConfig;
import com.xinchuang.entity.targetSystem.config.TargetElse;
import com.xinchuang.service.target.TargetSpecialAttributeService;
import com.xinchuang.service.targetSystem.config.SystemConfigService;
import com.xinchuang.util.RedisUtil;
import com.xinchuang.util.SnowflakeIdWorker;
import com.xinchuang.utils.MyUtil;
import com.xinchuang.utils.RedisName;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;

/**
 * @ClassName SystemConfigServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-08-09 10:12
 * @Verion V1.0
 */
@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    @Resource
    private SystemConfigMapper systemConfigMapper;

    @Resource
    private KeyWordSystem keyWordSystem;

    @Resource
    private TargetMapper targetMapper;

    @Resource
    private TargetElseMapper targetElseMapper;

    @Resource
    private TargetTypeMapper targetTypeMapper;

    @Resource
    private TargetSubTableMapper targetSubTableMapper;

    @Resource
    private TargetSpecialAttributeService targetSpecialAttributeService;

    @Resource
    private RedisUtil redisUtil;

    private SnowflakeIdWorker snow = new SnowflakeIdWorker(0, 0);

    @Override
    public Result addOrUpdateSystemConfig(SystemConfig systemConfig) {
        if (StringUtils.isNumeric(systemConfig.getName())) {
            return ResultUtil.error(400, "配置名称不能为纯数字，请修改后重试");
        }
        if (StringUtils.isBlank(systemConfig.getId()) || systemConfig.getId().equals("0")) {
            //校验名称唯一
            synchronized (this) {
                if (ObjectUtil.isNotEmpty(systemConfigMapper.selectSystemConfigOnly(systemConfig.getName()))) {
                    return ResultUtil.error(500, "关系类型名称已重复，请修改后重试");
                }
            }
            //添加ID
            systemConfig.setId(snow.nextId() + "");
            //系统配置默认状态为1
            systemConfig.setType(1);
            systemConfigMapper.addSystemConfig(systemConfig);
        } else {
            systemConfigMapper.updateSystemConfig(systemConfig);
        }
        return ResultUtil.success(null);
    }

    @Override
    public Result deleteSystemConfig(String id) {
        int i = systemConfigMapper.deleteSystemConfig(id);
        if (i > 0) {
            return ResultUtil.success("删除成功", null);
        }
        return ResultUtil.error(500, "删除数据不存在，请刷新列表后重试");
    }

    @Override
    public Result selectSystemConfig(Integer pageNum, Integer pageSize, Boolean isPull) {
        if (isPull) {
            List<Map<String, Object>> list = systemConfigMapper.selectSystemConfigPull();
            return ResultUtil.success(CollectionUtil.isNotEmpty(list) ? list : new ArrayList<>());
        } else {
            Map<String, Object> resultMap = new HashMap<>();
            PageHelper.startPage(pageNum, pageSize, "create_time DESC");
            List<SystemConfig> list = systemConfigMapper.selectSystemConfigList();
            PageInfo<SystemConfig> pageInfo = new PageInfo<>(list);
            if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
                List<Map<String, Object>> resultList = new ArrayList<>();
                Map<String, Object> map = null;
                for (SystemConfig systemConfig : pageInfo.getList()) {
                    map = new HashMap<>();
                    map.put("id", systemConfig.getId());
                    map.put("name", systemConfig.getName());
                    map.put("remark", systemConfig.getRemark());
                    map.put("color", systemConfig.getColor());
                    resultList.add(map);
                }
                resultMap.put("dataList", resultList);
            } else {
                resultMap.put("dataList", new ArrayList<>());
            }
            resultMap.put("pageNum", pageInfo.getPageNum());
            resultMap.put("pageSize", pageInfo.getPageSize());
            resultMap.put("total", pageInfo.getTotal());
            resultMap.put("pageAll", pageInfo.getPages());
            return ResultUtil.success(resultMap);
        }
    }

    @Override
    public void init() {
        Integer all = systemConfigMapper.selectSystemConfigAll();
        if (all == 0) {
            String[] strs = {"防护", "指挥", "通信", "支援", "防卫"};
            for (String str : strs) {
                SystemConfig systemConfig = new SystemConfig();
                String id = snow.nextId() + "";
                systemConfig.setId(id);
                systemConfig.setName(str);
                systemConfig.setColor("rgba(255, 255, 255, 1)");
                systemConfig.setType(1);
                systemConfigMapper.addSystemConfig(systemConfig);
            }
        }
    }

    @Override
    public void addTargetLevel(String targetId, String level) {
        if (StringUtils.isNotBlank(targetId)) {
            TargetElse targetElse = targetElseMapper.selectTargetElse(targetId, 1);
            if (ObjectUtil.isEmpty(targetElse)) {
                targetElse = new TargetElse();
                targetElse.setId(snow.nextId() + "");
                targetElse.setTargetId(targetId);
                targetElse.setName(level);
                targetElse.setType(1);
                targetElseMapper.addTargetElse(targetElse);
            } else {
                targetElse.setName(level);
                targetElseMapper.updateTargetElse(targetElse);
            }
        }
    }

    @Override
    public Result selectStatistics(Integer type, String id, Integer radarNear, Integer radarMiddle, Integer
            radarFar, Integer guidedNear, Integer guidedMiddle, Integer guidedFar) {
        Map<String, Object> resultMap = new HashMap<>();
        if (type == 1) {
            resultMap = statisticsTarget(radarNear, radarMiddle, radarFar, guidedNear, guidedMiddle, guidedFar);
        } else if (type == 2) {
            resultMap = statisticsSystem(id, radarNear, radarMiddle, radarFar, guidedNear, guidedMiddle, guidedFar);
        } else if (type == 3) {
            resultMap = statisticsSystems(id, radarNear, radarMiddle, radarFar, guidedNear, guidedMiddle, guidedFar);
        }
        return ResultUtil.success(resultMap);
    }

    public Map<String, Object> statisticsTarget(Integer radarNear, Integer radarMiddle, Integer radarFar, Integer
            guidedNear, Integer guidedMiddle, Integer guidedFar) {
        Map<String, Object> resultMap = new HashMap<>();
        //作用范围
        LinkedHashMap<String, Object> radarEffectScopeStat = systemConfigMapper.selectEffectScopeStat(radarNear, radarMiddle, radarFar, "雷达站");
        LinkedHashMap<String, Object> guidedEffectScopeStat = systemConfigMapper.selectEffectScopeStat(guidedNear, guidedMiddle, guidedFar, "导弹阵地");
        if (CollectionUtil.isNotEmpty(radarEffectScopeStat)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (String s : radarEffectScopeStat.keySet()) {
                map = new HashMap<>();
                map.put("name", s);
                map.put("value", radarEffectScopeStat.get(s));
                resultList.add(map);
            }
            resultMap.put("radarEffect", resultList);
        } else {
            resultMap.put("radarEffect", new ArrayList<>());
        }
        if (CollectionUtil.isNotEmpty(guidedEffectScopeStat)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (String s : guidedEffectScopeStat.keySet()) {
                map = new HashMap<>();
                map.put("name", s);
                map.put("value", guidedEffectScopeStat.get(s));
                resultList.add(map);
            }
            resultMap.put("guidedEffect", resultList);
        } else {
            resultMap.put("guidedEffect", new ArrayList<>());
        }
        //类型
        List<Map<String, Object>> typeStat = systemConfigMapper.selectTypeStat();
        Map<String, Object> typeMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(typeStat)) {
            List<String> xData = new ArrayList<>();
            List<Integer> yData = new ArrayList<>();
            for (Map<String, Object> map : typeStat) {
                xData.add(map.get("name").toString());
                yData.add(Integer.valueOf(map.get("value").toString()));
            }
            typeMap.put("xData", xData);
            typeMap.put("yData", yData);
        }
        resultMap.put("type", typeMap);
        //等级
        List<Map<String, Object>> stat = targetElseMapper.selectTypeStat(1);
        List<Map<String, Object>> levelResultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(stat)) {
            Map<String, Object> lMap = null;
            String[] levelList = {"一级", "二级", "三级"};
            for (String s : levelList) {
                lMap = new HashMap<>();
                lMap.put("name", s);
                for (Map<String, Object> map : stat) {
                    if (map.get("name").toString().equals(s)) {
                        lMap.put("value", map.get("value"));
                        break;
                    }
                }
                levelResultList.add(lMap);
            }
        }
        resultMap.put("level", levelResultList);
        resultMap.put("total", systemConfigMapper.selectTargetTotal());
        return resultMap;
    }

    public Map<String, Object> statisticsSystem(String id, Integer radarNear, Integer radarMiddle, Integer
            radarFar, Integer guidedNear, Integer guidedMiddle, Integer guidedFar) {
        Map<String, Object> resultMap = new HashMap<>();
        //作用范围
        LinkedHashMap<String, Object> radarEffectScopeStat = systemConfigMapper.selectEffectScopeStatSystem(id, radarNear, radarMiddle, radarFar, "雷达站");
        LinkedHashMap<String, Object> guidedEffectScopeStat = systemConfigMapper.selectEffectScopeStatSystem(id, guidedNear, guidedMiddle, guidedFar, "导弹阵地");
        if (CollectionUtil.isNotEmpty(radarEffectScopeStat)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (String s : radarEffectScopeStat.keySet()) {
                map = new HashMap<>();
                map.put("name", s);
                map.put("value", radarEffectScopeStat.get(s));
                resultList.add(map);
            }
            resultMap.put("radarEffect", resultList);
        } else {
            resultMap.put("radarEffect", new ArrayList<>());
        }
        if (CollectionUtil.isNotEmpty(guidedEffectScopeStat)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (String s : guidedEffectScopeStat.keySet()) {
                map = new HashMap<>();
                map.put("name", s);
                map.put("value", guidedEffectScopeStat.get(s));
                resultList.add(map);
            }
            resultMap.put("guidedEffect", resultList);
        } else {
            resultMap.put("guidedEffect", new ArrayList<>());
        }
        //类型
        List<Map<String, Object>> typeStat = systemConfigMapper.selectTypeStatSystem(id);
        Map<String, Object> typeMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(typeStat)) {
            List<String> xData = new ArrayList<>();
            List<Integer> yData = new ArrayList<>();
            for (Map<String, Object> map : typeStat) {
                xData.add(map.get("name").toString());
                yData.add(Integer.valueOf(map.get("value").toString()));
            }
            typeMap.put("xData", xData);
            typeMap.put("yData", yData);
        }
        resultMap.put("type", typeMap);
        //等级
        List<Map<String, Object>> stat = targetElseMapper.selectTypeStatSystem(1, id);
        List<Map<String, Object>> levelResultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(stat)) {
            Map<String, Object> lMap = null;
            String[] levelList = {"一级", "二级", "三级"};
            for (String s : levelList) {
                lMap = new HashMap<>();
                lMap.put("name", s);
                for (Map<String, Object> map : stat) {
                    if (map.get("name").toString().equals(s)) {
                        lMap.put("value", map.get("value"));
                        levelResultList.add(lMap);
                        break;
                    }
                }

            }
        }
        resultMap.put("level", levelResultList);
        resultMap.put("total", systemConfigMapper.selectSystemTargetTotal(id));
        return resultMap;
    }

    public Map<String, Object> statisticsSystems(String id, Integer radarNear, Integer radarMiddle, Integer
            radarFar, Integer guidedNear, Integer guidedMiddle, Integer guidedFar) {
        Map<String, Object> resultMap = new HashMap<>();
        //作用范围
        LinkedHashMap<String, Object> radarEffectScopeStat = systemConfigMapper.selectEffectScopeStatSystems(id, radarNear, radarMiddle, radarFar, "雷达站");
        LinkedHashMap<String, Object> guidedEffectScopeStat = systemConfigMapper.selectEffectScopeStatSystems(id, guidedNear, guidedMiddle, guidedFar, "导弹阵地");
        if (CollectionUtil.isNotEmpty(radarEffectScopeStat)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (String s : radarEffectScopeStat.keySet()) {
                map = new HashMap<>();
                map.put("name", s);
                map.put("value", radarEffectScopeStat.get(s));
                resultList.add(map);
            }
            resultMap.put("radarEffect", resultList);
        } else {
            resultMap.put("radarEffect", new ArrayList<>());
        }
        if (CollectionUtil.isNotEmpty(guidedEffectScopeStat)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (String s : guidedEffectScopeStat.keySet()) {
                map = new HashMap<>();
                map.put("name", s);
                map.put("value", guidedEffectScopeStat.get(s));
                resultList.add(map);
            }
            resultMap.put("guidedEffect", resultList);
        } else {
            resultMap.put("guidedEffect", new ArrayList<>());
        }
        //类型
        List<Map<String, Object>> typeStat = systemConfigMapper.selectTypeStatSystems(id);
        Map<String, Object> typeMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(typeStat)) {
            List<String> xData = new ArrayList<>();
            List<Integer> yData = new ArrayList<>();
            for (Map<String, Object> map : typeStat) {
                xData.add(map.get("name").toString());
                yData.add(Integer.valueOf(map.get("value").toString()));
            }
            typeMap.put("xData", xData);
            typeMap.put("yData", yData);
        }
        resultMap.put("type", typeMap);
        //等级
        List<Map<String, Object>> stat = targetElseMapper.selectTypeStatSystems(1, id);
        List<Map<String, Object>> levelResultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(stat)) {
            Map<String, Object> lMap = null;
            String[] levelList = {"一级", "二级", "三级"};
            for (String s : levelList) {
                lMap = new HashMap<>();
                lMap.put("name", s);
                for (Map<String, Object> map : stat) {
                    if (map.get("name").toString().equals(s)) {
                        lMap.put("value", map.get("value"));
                        levelResultList.add(lMap);
                        break;
                    }
                }

            }
        }
        resultMap.put("level", levelResultList);
        resultMap.put("total", systemConfigMapper.selectSystemsTargetTotal(id));
        return resultMap;
    }

    @Override
    public Result addDataMessage(String targetId, String subId, String id, String name, String url, String
            remark, String fileList) {
        TargetElse targetElse = new TargetElse();
        if (StringUtils.isBlank(id) || id.equals("0")) {
            targetElse.setId(snow.nextId() + "");
        } else {
            targetElse.setId(id);
        }
        targetElse.setTargetId(targetId);
        if (StringUtils.isNotBlank(subId)) {
            targetElse.setSubId(subId);
        }
        targetElse.setName(name);
        targetElse.setStatus(url);
        targetElse.setRemark(remark);
        targetElse.setMessage(fileList);
        targetElse.setType(2);
        if (StringUtils.isBlank(id) || id.equals("0")) {
            targetElseMapper.addTargetElse(targetElse);
        } else {
            targetElseMapper.updateTargetElse(targetElse);
        }
        return ResultUtil.success();
    }

    @Override
    public Result deleteDataMessage(String id) {
        TargetElse targetElse = targetElseMapper.selectTargetElseById(id);
        if (ObjectUtil.isNotNull(targetElse)) {
            targetElseMapper.deleteTargetElse(id);
            FileUtil.del(this.keyWordSystem.getFILE_URL() + targetElse.getStatus());
        }
        return ResultUtil.success("删除成功", null);
    }

    @Override
    public Result selectDataMessage(String targetId) {
        List<TargetElse> targetElses = targetElseMapper.selectTargetElseByTargetIdAndType(targetId, 2);
        if (CollectionUtil.isNotEmpty(targetElses)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (TargetElse targetEls : targetElses) {
                map = new HashMap<>();
                map.put("id", targetEls.getId());
                map.put("name", targetEls.getName());
                map.put("fileUrl", targetEls.getStatus());
                map.put("remark", targetEls.getRemark());
                map.put("fileList", targetEls.getMessage());
                resultList.add(map);
            }
            return ResultUtil.success(resultList);
        }
        return ResultUtil.success(new ArrayList<>());
    }

    @Override
    public Result selectDataMessageBySubId(String targetId, String subId) {
        List<TargetElse> targetElses = targetElseMapper.selectTargetElseByTargetIdAndTypeBySubId(targetId, subId, 2);
        if (CollectionUtil.isNotEmpty(targetElses)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (TargetElse targetEls : targetElses) {
                map = new HashMap<>();
                map.put("id", targetEls.getId());
                map.put("name", targetEls.getName());
                map.put("fileUrl", targetEls.getStatus());
                map.put("remark", targetEls.getRemark());
                map.put("fileList", targetEls.getMessage());
                resultList.add(map);
            }
            return ResultUtil.success(resultList);
        }
        return ResultUtil.success(new ArrayList<>());
    }

    @Override
    public Result selectDataMessageById(String id) {
        TargetElse targetElses = targetElseMapper.selectTargetElseById(id);
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isNotNull(targetElses)) {
            map.put("id", targetElses.getId());
            map.put("name", targetElses.getName());
            map.put("fileUrl", targetElses.getStatus());
            map.put("remark", targetElses.getRemark());
            map.put("fileList", targetElses.getMessage());
        }
        return ResultUtil.success(map);
    }

    @Override
    public Result addSignMessage(String targetId, String subId, String id, String name, String url, String
            message, String icon, String remark) {
        TargetElse targetElse = new TargetElse();
        if (StringUtils.isBlank(id) || id.equals("0")) {
            targetElse.setId(snow.nextId() + "");
        } else {
            targetElse.setId(id);
        }
        targetElse.setTargetId(targetId);
        if (StringUtils.isNotBlank(subId)) {
            targetElse.setSubId(subId);
        }
        targetElse.setName(name);
        targetElse.setAddress(url);
        targetElse.setMessage(message);
        targetElse.setStatus(icon);
        targetElse.setRemark(remark);
        targetElse.setType(3);
        if (StringUtils.isBlank(id) || id.equals("0")) {
            targetElseMapper.addTargetElse(targetElse);
        } else {
            targetElseMapper.updateTargetElse(targetElse);
        }
        return ResultUtil.success();
    }

    @Override
    public Result deleteSignMessage(String id) {
        targetElseMapper.deleteTargetElse(id);
        return ResultUtil.success("删除成功", null);
    }

    @Override
    public Result selectSignMessage(String targetId) {
        List<TargetElse> targetElses = targetElseMapper.selectTargetElseByTargetIdAndType(targetId, 3);
        if (CollectionUtil.isNotEmpty(targetElses)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (TargetElse targetEls : targetElses) {
                map = new HashMap<>();
                map.put("id", targetEls.getId());
                map.put("name", targetEls.getName());
                map.put("address", targetEls.getAddress());
                map.put("reveal", targetEls.getMessage());
                map.put("icon", targetEls.getStatus());
                map.put("remark", targetEls.getRemark());
                resultList.add(map);
            }
            return ResultUtil.success(resultList);
        }
        return ResultUtil.success(new ArrayList<>());
    }

    @Override
    public Result selectSignMessageBySubId(String targetId, String subId) {
        List<TargetElse> targetElses = targetElseMapper.selectTargetElseByTargetIdAndTypeBySubId(targetId, subId, 3);
        if (CollectionUtil.isNotEmpty(targetElses)) {
            List<Map<String, Object>> resultList = new ArrayList<>();
            Map<String, Object> map = null;
            for (TargetElse targetEls : targetElses) {
                map = new HashMap<>();
                map.put("id", targetEls.getId());
                map.put("name", targetEls.getName());
                map.put("address", targetEls.getAddress());
                map.put("reveal", targetEls.getMessage());
                map.put("icon", targetEls.getStatus());
                map.put("remark", targetEls.getRemark());
                resultList.add(map);
            }
            return ResultUtil.success(resultList);
        }
        return ResultUtil.success(new ArrayList<>());
    }

    @Override
    public Result selectSignMessageById(String id) {
        TargetElse targetElses = targetElseMapper.selectTargetElseById(id);
        Map<String, Object> map = new HashMap<>();
        if (ObjectUtil.isNotNull(targetElses)) {
            map.put("id", targetElses.getId());
            map.put("name", targetElses.getName());
            map.put("address", targetElses.getAddress());
            map.put("reveal", targetElses.getMessage());
            map.put("remark", targetElses.getRemark());
        }
        return ResultUtil.success(map);
    }

    @Override
    public synchronized Result copyTypeOrSubType(JSONObject json) {
        String sourceId = json.getString("sourceId");
        Integer type = json.getInteger("type");
        String targetId = json.getString("targetId");
        if (1 == type && StringUtils.isBlank(sourceId)) {
            return ResultUtil.error(500, "请选择需要复制的目标/子目标类型");
        } else if (1 == type && StringUtils.isNotBlank(sourceId)){
            redisUtil.set(RedisName.COPY_TYPE_ID, sourceId, 60 * 60);
            return ResultUtil.success("复制成功", null);
        } else if (2 == type) {
            if (!redisUtil.hasKey(RedisName.COPY_TYPE_ID)) {
                return ResultUtil.error(500, "请选择需要复制的目标/子目标类型");
            }
            if (StringUtils.isBlank(targetId)) {
                return ResultUtil.error(500, "请选择需要粘贴的目标/子目标类型");
            }
            String typeId = redisUtil.get(RedisName.COPY_TYPE_ID).toString();
            List<TargetSpecialAttribute> attributes = targetSpecialAttributeService.lambdaQuery().eq(TargetSpecialAttribute::getTypeId, targetId).list();
            if (CollectionUtil.isNotEmpty(attributes)) {
                return ResultUtil.error(500, "该类型已有属性数据，如需粘贴请清空后重试");
            }
            attributes = targetSpecialAttributeService.lambdaQuery().eq(TargetSpecialAttribute::getTypeId, typeId).list();
            if (CollectionUtil.isEmpty(attributes)) {
                return ResultUtil.error(500, "复制的类型属性为空，请检查后重试");
            } else {
                Set<String> typeNameSet = new HashSet<>();
                for (TargetSpecialAttribute attribute : attributes) {
                    typeNameSet.add(attribute.getKeyValue());
                }
                for (TargetSpecialAttribute attribute : attributes) {
                    attribute.setId(snow.nextId() + "");
                    attribute.setTypeId(targetId);
                    attribute.setKeyValue(getRandomName(typeNameSet));
                }
                targetSpecialAttributeService.saveBatch(attributes);
                return ResultUtil.success();
            }
        }
        return ResultUtil.error(500, "未知异常，请稍后重试");
     }

    private String getRandomName(Set<String> set) {
        String str = MyUtil.getRandomStr(8);
        if (set.contains(str)) {
            return getRandomName(set);
        }
        set.add(str);
        return str;
    }

}